COBJS := $(patsubst %.c, %.o, $(wildcard *.c))
ASMOBJS := $(patsubst %.S, %.o, $(wildcard *.S))

OBJS := $(COBJS)
DETS := $(patsubst %.o, %.det, $(OBJS))

MYOBJS := $(addprefix $(COMP_BUILD_DIR)/$(COMPONENT)/$(SUBCOMPONENT)/, $(OBJS))
MYOBJS += $(addprefix $(COMP_BUILD_DIR)/$(COMPONENT)/$(SUBCOMPONENT)/, $(ASMOBJS))
MYDETS := $(addprefix $(COMP_BUILD_DIR)/$(COMPONENT)/$(SUBCOMPONENT)/, $(DETS))

MYLOGS := $(addsuffix .log, $(MYOBJS))

TARGET_DEP := $(COMP_BUILD_DIR)/$(COMPONENT)/$(SUBCOMPONENT)/$(SUBCOMPONENT).dep
TARGET_OBJ := $(COMP_BUILD_DIR)/$(COMPONENT)/$(SUBCOMPONENT).tmp.o

vpath 
vpath %.c $(PWD)/$(COMPONENT)/$(SUBCOMPONENT)
vpath %.h $(PWD)/include $(PWD)/include/cnix
vpath %.o $(COMP_BUILD_DIR)/$(COMPONENT)/$(SUBCOMPONENT)
vpath %.det $(COMP_BUILD_DIR)/$(COMPONENT)/$(SUBCOMPONENT)

all: $(TARGET_OBJ)

$(TARGET_OBJ):	$(OBJS) $(ASMOBJS)
	@$(LD) $(LDFLAGS) -o $@ $(MYOBJS)

%.o: %.c 
	@$(CC) $(CFLAGS) -c $< -o $(COMP_BUILD_DIR)/$(COMPONENT)/$(SUBCOMPONENT)/$@
%.o: %.S
	@$(CC) $(CFLAGS) -c $< -o $(COMP_BUILD_DIR)/$(COMPONENT)/$(SUBCOMPONENT)/$@

#%.d: %.c
#	rm -f $@ ; \
	$(CC) $(CFLAGS) -MM $< >$@.$$$$; \
	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

%.det: %.c
	@rm -f $(COMP_BUILD_DIR)/$(COMPONENT)/$(SUBCOMPONENT)/$@ ; \
	$(CC) $(CFLAGS) -MM $< >$(COMP_BUILD_DIR)/$(COMPONENT)/$(SUBCOMPONENT)/$@.$$$$; \
	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $(COMP_BUILD_DIR)/$(COMPONENT)/$(SUBCOMPONENT)/$@.$$$$ > $(COMP_BUILD_DIR)/$(COMPONENT)/$(SUBCOMPONENT)/$@; \
	rm -f $(COMP_BUILD_DIR)/$(COMPONENT)/$(SUBCOMPONENT)/$@.$$$$

-include $(TARGET_DEP)

dep: $(TARGET_DEP)

$(TARGET_DEP): $(DETS)
	@if [ -f $(TARGET_DEP) ]; then rm -f $(TARGET_DEP); fi
	@cat $(COMP_BUILD_DIR)/$(COMPONENT)/$(SUBCOMPONENT)/*.det >> $(TARGET_DEP)

clean:
	@rm -f *.o .*.swp *~ .tmp.o *.dep $(MYOBJS) $(MYDETS) $(TARGET_DEP) $(TARGET_OBJ)
